package com.totofgeek.contactsboard.widget.outils;

import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.preference.PreferenceManager;
import android.provider.ContactsContract;
import android.provider.ContactsContract.QuickContact;
import android.widget.Toast;

import com.totofgeek.contactsboard.R;

public abstract class AbstractWidgetProvider extends AppWidgetProvider
{
	public static final String	PARAM_ACTION		= "com.totofgeek.contactsboard.widget.TOAST_ACTION";
	public static final String	EXTRA_CONTACT_ID	= "com.totofgeek.contactsboard.widget.EXTRA_ITEM";

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.appwidget.AppWidgetProvider#onDeleted(android.content.Context, int[])
	 */
	@Override
	public void onDeleted(Context context, int[] appWidgetIds)
	{
		super.onDeleted(context, appWidgetIds);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.appwidget.AppWidgetProvider#onDisabled(android.content.Context)
	 */
	@Override
	public void onDisabled(Context context)
	{
		super.onDisabled(context);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.appwidget.AppWidgetProvider#onEnabled(android.content.Context)
	 */
	@Override
	public void onEnabled(Context context)
	{
		super.onEnabled(context);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.appwidget.AppWidgetProvider#onReceive(android.content.Context, android.content.Intent)
	 */
	@Override
	public void onReceive(Context context, Intent intent)
	{
		super.onReceive(context, intent);

		if (intent.getAction().equals(PARAM_ACTION))
		{
			SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
			int mode;
			// FIXME Utiliser une constante
			String valeurPreference = sharedPrefs.getString(context.getString(R.string.pref_widget_cle), "Normale");
			if ("Petite".equals(valeurPreference))
				mode = ContactsContract.QuickContact.MODE_SMALL;
			else if ("Normale".equals(valeurPreference))
				mode = ContactsContract.QuickContact.MODE_MEDIUM;
			else if ("Grande".equals(valeurPreference))
				mode = ContactsContract.QuickContact.MODE_LARGE;
			else
				mode = ContactsContract.QuickContact.MODE_MEDIUM;

			Uri uri = intent.getData();
			if (uri != null)
				QuickContact.showQuickContact(context, intent.getSourceBounds(), uri, mode, null);
			else
				Toast.makeText(context, R.string.probleme_affichage_badge_contact, Toast.LENGTH_SHORT).show();
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.appwidget.AppWidgetProvider#onUpdate(android.content.Context, android.appwidget.AppWidgetManager, int[])
	 */
	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
	{
		super.onUpdate(context, appWidgetManager, appWidgetIds);
	}
}